
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ManualMassDetect implements ActionListener
{
	IPWindow window;
	JFrame frame;
	JPanel mainPane;

	public ManualMassDetect(IPWindow _window)
	{
		window = _window;
		Color bgColor = new Color(255,255,255);
		
		// Create our frame, panel and buttons.
		frame = new JFrame("Mass Detect");
		mainPane = new JPanel(new BorderLayout());

		// Setup main pane
		mainPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		// Action panel
		JPanel actionPanel = new JPanel();
		actionPanel.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder("   "),
					BorderFactory.createEmptyBorder(5,5,5,5)
					));
		actionPanel.setBackground(bgColor);
		JButton actionMassDetect = new JButton("Perform Mass Detection");
		actionMassDetect.setToolTipText("Perform mass detection");
		JButton actionRegionGrow = new JButton("Perorm Region Growing");
		actionMassDetect.setActionCommand("massDetect");
		actionRegionGrow.setActionCommand("regionGrow");
		actionMassDetect.addActionListener(this);
		actionRegionGrow.addActionListener(this);
		actionPanel.add(actionMassDetect);
		actionPanel.add(actionRegionGrow);

		// Nav buttons
		JPanel navPane = new JPanel();
		JButton b1 = new JButton("Prev");
		JButton b2 = new JButton("Next");
		JButton b3 = new JButton("Close");
		b1.setActionCommand("prev");
		b2.setActionCommand("next");
		b3.setActionCommand("close");
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		navPane.add(b1);
		navPane.add(b2);
		navPane.add(b3);
		navPane.setBackground(bgColor);

		mainPane.add(actionPanel, BorderLayout.PAGE_START);
		mainPane.add(navPane, BorderLayout.PAGE_END);
		
		// Set panel as mane pane on frame, and show frame.
		frame.setContentPane(mainPane);
		mainPane.setOpaque(true);
		mainPane.setBackground(bgColor);
		frame.pack();
		//frame.setLocationRelativeTo(window.frame);
		frame.setVisible(true);		

		////
		// Warning messges
		if (window.getROI() == null)
		{
			p.print("Warning: ROI is null.  Verfiy image is loaded.");
		}
		else if (window.getROI().getBinaryImage() == null)
		{
			p.print("Warning: Binary image is null. Verfiy " +
					"thresholding was performed in previous step");
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		if (cmd.equals("massDetect"))
		{
			p.print("Performing Mass Detection");
			window.getROI().doMassDetect();
			window.frame.setImageState(2); // show labeled image
			window.updateFrame();
		}
		else if (cmd.equals("regionGrow"))
		{
			p.print("Performing Region Growing");
			window.getROI().doRegionGrowing();
			window.updateFrame();
		}
		else if (cmd.equals("prev"))
		{
			new ManualThreshold(window);
			frame.dispose();
		}
		else if (cmd.equals("next"))
		{
			new ManualLabeledMasses(window);
			frame.dispose();
		}
		else if (cmd.equals("close"))
		{
			frame.dispose();
		}
	}
}
